package org.example.myleet.p926;

public class Solution {
    public int minFlipsMonoIncr(String s) {
        char[] charArr = s.toCharArray();
        int lastRoundTo0, lastRoundTo1;
        char c = charArr[0];
        if (c == '0') {
            lastRoundTo0 = 0;
            lastRoundTo1 = 1;
        } else {
            lastRoundTo0 = 1;
            lastRoundTo1 = 0;
        }
        for (int i = 1; i < charArr.length; ++i) {
            c = charArr[i];
            int thisRoundTo0, thisRoundTo1;
            if (c == '0') {
                thisRoundTo0 = lastRoundTo0;
                thisRoundTo1 = Math.min(lastRoundTo0, lastRoundTo1) + 1;
            } else {
                thisRoundTo0 = lastRoundTo0 + 1;
                thisRoundTo1 = Math.min(lastRoundTo0, lastRoundTo1);
            }
            lastRoundTo0 = thisRoundTo0;
            lastRoundTo1 = thisRoundTo1;
        }
        return Math.min(lastRoundTo0, lastRoundTo1);
    }

//    public int minFlipsMonoIncr(String s) {
//        char[] charArr = s.toCharArray();
//        int[][] dp = new int[charArr.length][2];
//        char c = charArr[0];
//        if (c == '0') {
//            dp[0][0] = 0;
//            dp[0][1] = 1;
//        } else {
//            dp[0][0] = 1;
//            dp[0][1] = 0;
//        }
//        for (int i = 1; i < charArr.length; ++i) {
//            c = charArr[i];
//            if (c == '0') {
//                dp[i][0] = dp[i - 1][0];
//                dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][1]) + 1;
//            } else {
//                dp[i][0] = dp[i - 1][0] + 1;
//                dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][1]);
//            }
//        }
//        return Math.min(dp[charArr.length - 1][0], dp[charArr.length - 1][1]);
//    }
}
